package com.threeox.commonlibrary.entity.engine.event;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.entity.base.BaseObj;
import com.threeox.commonlibrary.ui.view.inter.engine.model.IBaseViewControl;
import com.threeox.commonlibrary.ui.view.inter.multipage.IMultiPageView;
import com.threeox.commonlibrary.util.FragmentUtils;
import com.threeox.commonlibrary.util.RegexHelper;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;
import com.threeox.utillibrary.util.res.StringHelper;

import java.lang.reflect.Constructor;

/**
 *
 * @ClassName: ViewMessage
 *
 * @Description: TODO(View 配置信息)
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/7/4 17:56
 *
 * @version 1.0
 *
 */
public class ViewMessage extends BaseObj {

    private JSONObject params;// 传递参数
    private Class viewClass;// view 的Class
    private String viewClassName;// view 的Class全名称
    private String paramsModelName;// 请求参数的模型名称

    private JSONObject paramsKeys;// 传递参数替换的

    public void initData(StringHelper stringHelper) {
        try {
            RegexHelper.replaceJSONByRawName(paramsModelName, params);
            if (EmptyUtils.isNotEmpty(viewClassName)) {
                this.viewClassName = stringHelper.getStringText(viewClassName);
                this.viewClass = Class.forName(this.viewClassName);
            }
        } catch (Exception e) {
        }
        RegexHelper.replaceSys(params);
        this.paramsKeys = RegexHelper.getReplaceKeys(params);
    }

    public JSONObject getParams() {
        return params;
    }

    public void setParams(JSONObject params) {
        this.params = params;
    }

    public Class getViewClass() {
        return viewClass;
    }

    public void setViewClass(Class viewClass) {
        this.viewClass = viewClass;
    }

    public String getViewClassName() {
        return viewClassName;
    }

    public void setViewClassName(String viewClassName) {
        this.viewClassName = viewClassName;
    }

    public JSONObject getParamsKeys() {
        return paramsKeys;
    }

    public void setParamsKeys(JSONObject paramsKeys) {
        this.paramsKeys = paramsKeys;
    }

    public String getParamsModelName() {
        return paramsModelName;
    }

    public void setParamsModelName(String paramsModelName) {
        this.paramsModelName = paramsModelName;
    }

    /**
     * 初始化參數
     *
     * @param data
     * @return
     */
    public void initParam(Object data) {
        RegexHelper.replace(this.params, paramsKeys, data);
    }

    /**
     * 初始化View
     */
    public View initView(Context context) {
        try {
            Class viewClass = getViewClass();
            // 得到ViewCls 带有Context参数的构造方法
            Constructor<?> constructor = viewClass.getConstructor(Context.class);
            IMultiPageView viewControl = (IMultiPageView) constructor.newInstance(context);
            viewControl.initView(getParams());
            return (View) viewControl;
        } catch (Exception e) {
            LogUtils.e(TAG, e);
        }
        return null;
    }

}
